﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace Login
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //doorgegeven parameters
            String name = txtUsername.Text;
            String pass = txtPassword.Text;

            if (loginCheck(name, pass) == true)
            {
                this.Dispose(false);
                //Projectkeuzemenu.Projectkeuzemenu keuze = new Projectkeuzemenu.Projectkeuzemenu(name, pass);
                WindowsFormsApplication5.Form1 loading = new WindowsFormsApplication5.Form1(name, pass);
                //keuze.Show();
                loading.Show();
            }
            else
            {
                MessageBox.Show("Failed to log in");
            }
        }

        public Boolean loginCheck(string email, string wachtwoord)
        {
            string username = email;
            string password = wachtwoord;
            
            try
            {
                //connectie naar server + query
                MySqlConnection grabConnection = new MySqlConnection("Server=instance8003.db.xeround.com;Port=6006;Database=grabcollab;Uid=cookicom0;Pwd=pizzahut91;");
                MySqlCommand login = new MySqlCommand("SELECT Email, Wachtwoord FROM confirmed_users WHERE Email='" + username + "' AND Wachtwoord='" + password + "'", grabConnection);
                grabConnection.Open();

                //data lezen
                MySqlDataReader reader = login.ExecuteReader();

                if (reader.Read() != false)
                {
                    if (reader.IsDBNull(0) == true)
                    {
                        //alles sluiten, login is gefaald
                        grabConnection.Close();
                        reader.Dispose();
                        login.Dispose();
                        grabConnection.Close();
                        return false;
                    }
                    else
                    {
                        //login geslaagd!
                        grabConnection.Close();
                        reader.Dispose();
                        login.Dispose();
                        grabConnection.Close();
                        return true;
                    }
                }
                else
                {
                    grabConnection.Close();
                    return false;
                }  
            }
            catch (MySqlException)
            {
                MessageBox.Show("Sql error detected!");
                return false;
            }
        }

        private void txtUsername_TextChanged(object sender, EventArgs e)
        {

        }

        private void Login_Load(object sender, EventArgs e)
        {

        }
    }
}

